﻿//********************************************************************************//
//创建日期：<创建日期，2016-05-28>
//创建作者：<郭登杰>
//功能说明：来函水表信息列表
//********************************************************************************//
using BLL.Access;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Web.Class;
using Entity;
using BLL;
using DevExpress.Web;

namespace Web.Controls
{
    public partial class UCLHShuiBiaoListQS : System.Web.UI.UserControl, IUCLHOperate
    {
        #region 属性

        /// <summary>
        /// 记录查询结果集
        /// </summary>
        private DataTable dtSource
        {
            get
            {
                return ViewState["dtSource"] as DataTable;
            }
            set
            {
                ViewState["dtSource"] = value;
            }
        }

        /// <summary>
        /// 来函类型
        /// </summary>
        public eLaiHanLX LaiHanLX { get; set; }

        /// <summary>
        /// 来函流程步骤
        /// </summary>
        public eLaiHanLCBH LaiHanLC {
            get
            {
                return ViewState["LaiHanLC"]==null?eLaiHanLCBH.未登记:(eLaiHanLCBH)Enum.Parse(typeof(eLaiHanLCBH), Convert.ToString(ViewState["LaiHanLC"])); 
            }
            set
            {
                ViewState["LaiHanLC"] = value.ToString();
            }        
        }

        /// <summary>
        /// 设置复选框列显隐
        /// </summary>
        public bool ShowCheck;

        /// <summary>
        /// 记录来函编号
        /// </summary>
        public string LaiHanBH 
        {
            get 
            {
                return Convert.ToString(ViewState["LaiHanBH"]);
            }
            set
            {
                ViewState["LaiHanBH"] = value;
            }
        }
        /// <summary>
        /// 记录井原用户号
        /// </summary>
        public string YuanYongHuH
        {
            get
            {
                return Convert.ToString(ViewState["YuanYongHuH"]);
            }
            set
            {
                ViewState["YuanYongHuH"] = value;
            }
        }
        /// <summary>
        /// 记录目标用户号
        /// </summary>
        public string TargetYongHuH
        {
            get
            {
                return Convert.ToString(ViewState["TargetYongHuH"]);
            }
            set
            {
                ViewState["TargetYongHuH"] = value;
            }
        }
        /// <summary>
        /// 过户水表号
        /// </summary>
        public string S_SHUIBIAOBH
        {
            get
            {
                return Convert.ToString(ViewState["S_SHUIBIAOBH"]);
            }
            set
            {
                ViewState["S_SHUIBIAOBH"] = value;
            }
        }

        /// <summary>
        /// 设置列表高度
        /// </summary>
        public int ListHeight
        {
            set
            {
                this.gvList.Settings.VerticalScrollableHeight = value;
            }
        }

        #endregion

        #region 事件

        /// <summary>
        /// 页面加载事件
        /// </summary>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                InitControl();
            }
        }

        /// <summary>
        /// 移除
        /// </summary>
        protected void lnbDel_Command(object sender, CommandEventArgs e)
        {
            //清除结果集中对应记录
            for (int i = 0; i < this.dtSource.Rows.Count; i++)
            {
                if (this.dtSource.Rows[i]["S_SHUIBIAOBH"].ToString() == e.CommandArgument.ToString())
                {
                    this.dtSource.Rows.RemoveAt(i);
                }
            }
            this.BindData();
        }

        #endregion

        #region 方法

        /// <summary>
        /// 配置控件
        /// </summary>
        public void InitControl()
        {
            this.gvList.Columns[2].Caption = BusinessTool.WType == 1 ? "取水口号" : "井号";
            this.gvList.Columns[3].Visible = this.LaiHanLX == eLaiHanLX.水资源销表;


           
        }

        /// <summary>
       ///重新绑定
        /// </summary>
        public void BindData()
        {
            this.gvList.DataSource = this.dtSource;
            this.gvList.DataBind();
            //控制复选框列
            for (int i = 0; i < this.gvList.VisibleRowCount; i++)
            {
                ASPxCheckBox cb = (ASPxCheckBox)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "cb");
                if (null != cb) cb.Visible = ShowCheck;
            }
        }
        private void SetChecked(bool check)
        {
            //控制复选框列
            for (int i = 0; i < this.gvList.VisibleRowCount; i++)
            {
                ASPxCheckBox cb = (ASPxCheckBox)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "cb");
                cb.Checked = check;
            }
        }

        /// <summary>
        /// 清空数据
        /// </summary>
        public void ClearInfo()
        {
            this.dtSource = null;
            this.gvList.DataSource = null;
            this.gvList.DataBind();
        }

        /// <summary>
        /// 验证
        /// </summary>
        /// <returns></returns>
        public ValidateRS Validate()
        {
            ValidateRS rs = new ValidateRS();

            if (this.LaiHanLX == eLaiHanLX.水资源销表)
            {
                if (this.dtSource==null||this.dtSource.Rows.Count == 0)
                {
                    rs.Msg = "请添加要销的表。";
                    return rs;
                }
                else
                {
                    if (this.SelectedShuiBiaoList().Count == 0)
                    {
                        rs.Msg = "请选择要销的水表。";
                        return rs;
                    }
                }
            }
            else if (this.LaiHanLX == eLaiHanLX.水资源表过户)
            {
                if (this.dtSource == null || this.dtSource.Rows.Count == 0)
                {
                    rs.Msg = "请将表信息及目标用户信息输入完整。";
                    return rs;
                }
                if(this.gvList.FocusedRowIndex==-1)
                {
                    rs.Msg = "请选择目标井。";
                    return rs;
                }
            }
            rs.IsSuccess = true;
            return rs;
        }

        public ValidateRS ValidateAdd(string shuibiaobh,string yonghuh)
        {
            ValidateRS rs = new ValidateRS();

            if (this.LaiHanLX == eLaiHanLX.水资源销表)
            {
                List<QS_LH_XIAOBIAOSBXX> list = this.SelectedShuiBiaoList();
                foreach (var item in list)
                {
                    if(item.S_SHUIBIAOBH==shuibiaobh)
                    {
                        rs.IsSuccess = false;
                        rs.Msg = "该水表已在列表中";
                        return rs;
                    }
                }
                foreach (var item in list)
                {
                    if (item.S_YONGHUH != yonghuh)
                    {
                        rs.IsSuccess = false;
                        rs.Msg = "同一来函要销的表需在同一用户下";
                        return rs;
                    }
                }
                
            }
            rs.IsSuccess = true;
            return rs;
        }

        public void Add(string shuibiaobh,string jbh, string yonghuh)
        {
            if (this.dtSource == null)
            {
                this.dtSource = new DataTable();
                this.dtSource.Columns.Add("S_SHUIBIAOBH");
                this.dtSource.Columns.Add("S_QUSHUIK");
                this.dtSource.Columns.Add("S_YONGHUH");
            }
            DataRow row=this.dtSource.NewRow();
            row["S_SHUIBIAOBH"] = shuibiaobh;
            row["S_QUSHUIK"] = jbh;
            row["S_YONGHUH"] = yonghuh;
            this.dtSource.Rows.Add(row);
            this.BindData();
            //this.SetChecked(true);
        }

        /// <summary>
        /// 获取所有选中的水表信息
        /// </summary>
        /// <returns></returns>
        private List<QS_LH_XIAOBIAOSBXX> SelectedShuiBiaoList()
        {
            List<QS_LH_XIAOBIAOSBXX> list = new List<QS_LH_XIAOBIAOSBXX>();
            for (int i = 0; i < this.gvList.VisibleRowCount; i++)
            {
                ASPxCheckBox cb = (ASPxCheckBox)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "cb");
                HiddenField hSBBH = (HiddenField)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "hfShuiBiaoBH");
                HiddenField hYHH = (HiddenField)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "hfYONGHUH");
                if (cb.Checked||!cb.Visible)
                {
                    QS_LH_XIAOBIAOSBXX item = new QS_LH_XIAOBIAOSBXX();
                    item.S_SHUIBIAOBH = hSBBH.Value;
                    item.S_YONGHUH = hYHH!=null?hYHH.Value:null;
                    list.Add(item);
                }
            }
            return list;
        }

        /// <summary>
        /// 获取来函主表信息
        /// </summary>
        /// <param name="laihanid">ID</param>
        /// <param name="liuchengzt">0:收件 1:收件并登记</param>
        /// <param name="msg">输出消息,正常为空</param>
        /// <returns>null:验证失败 !null:成功返回</returns>
        public YS_LH_LAIHANXX GetLAIHANXX(int laihanid, int liuchengzt, out string msg)
        {
            YS_LH_LAIHANXX laihan = new YS_LH_LAIHANXX();
            string laihanbh = "";
            string xukebh = "";
            msg = "";
            laihan.ID = laihanid;
            if (laihanid == 0)
            {
                string zd, glk, ywy;
                PageTools.GetLHDJZDGLKYWY(this.YuanYongHuH, out zd, out glk, out ywy);
                laihan.S_ZHANDIANBH = zd;
                laihan.S_GUANLIKBH = glk;
                laihan.I_YWY = int.Parse(ywy);
                laihan.S_YWY = BusinessTool.GetYWYName(ywy);
                //laihan.S_YHYWY = ywy;
                //DataSet numset = new SpHelper().P_YS_LH_XUKEBH(glk, 1, 0, (BusinessTool.GetWaterYM() / 100).ToString(), out xukebh, out laihanbh);
                new SpHelper().P_ALL_LH_BHSC(BusinessTool.WType, (int)this.LaiHanLX, BusinessTool.GetWaterYM() / 100, out laihanbh);
                laihan.I_XT = BusinessTool.WType;
                laihan.D_SHURURQ = DateTime.Now;
                laihan.D_LAIHANRQ = DateTime.Now;
                laihan.I_JLZT = 0;
                laihan.I_LAIHANLX = (int)this.LaiHanLX;
                laihan.I_SHURUR = LoginInfo.CurrentUser.UserID;
                laihan.I_AUTOTYPE = 0;
                laihan.S_LAIHANBH = laihanbh;
                laihan.S_BEIYONG1 = xukebh;
                laihan.S_XUKEBH = xukebh;
                laihan.I_LIUCHENGZT = (int)eLaiHanLCZT.流程中;
                laihan.I_Y = BusinessTool.GetWaterYM() / 100;
                laihan.I_M = BusinessTool.GetWaterYM() % 100;
                laihan.S_YONGHUBH = this.YuanYongHuH;
                laihan.S_YONGHUM=LaiHanLCTools.GetYONGHUM_QS(this.YuanYongHuH);
                if(this.LaiHanLX==eLaiHanLX.水资源销表)
                {
                    List<QS_LH_XIAOBIAOSBXX> listT=this.SelectedShuiBiaoList();
                    string[] sbbhs=new string[listT.Count];
                    for (int i = 0; i < sbbhs.Length; i++)
			        {
			            sbbhs[i]=listT[i].S_SHUIBIAOBH;
			        }
                    laihan.S_BEIZHU=string.Join(",",sbbhs);

                }
                else if(this.LaiHanLX==eLaiHanLX.水资源表过户)
                {
                    laihan.S_BEIZHU=this.S_SHUIBIAOBH;
                }
            }
            else
            {
                laihan = new YS_LH_LAIHANXXProcess().SelectYS_LH_LAIHANXX(laihan.ID);
            }
            laihan.I_LIUCHENGBH = liuchengzt;
            laihan.S_CAOZUOR = LoginInfo.CurrentUser.UserID.ToString();
            laihan.D_CAOZUOSJ = BusinessTool.GetDataBaseDateTime();
            return laihan;
        }

        /// <summary>
        /// 根据来函加载信息
        /// </summary>
        /// <param name="laihanxx"></param>
        public void LoadInfo(YS_LH_LAIHANXX laihanxx)
        {
            if (laihanxx.ID == 0)
            {
                this.LaiHanLC = eLaiHanLCBH.未登记;
                if (string.IsNullOrEmpty(laihanxx.S_YONGHUBH))
                {
                    return;
                }
                else
                {
                    if (laihanxx.S_YONGHUBH.Length==6)
                    {
                        laihanxx.S_YONGHUBH = "0" + laihanxx.S_YONGHUBH;
                    }
                }
            }
            else
            {
                this.LaiHanLC = (eLaiHanLCBH)Enum.Parse(typeof(eLaiHanLCBH),laihanxx.I_LIUCHENGBH.ToString());
                if(this.LaiHanLC>=eLaiHanLCBH.登记)
                {
                    this.gvList.Columns[3].Visible = false;
                }
            }
            DataTable dt = null;
            if (laihanxx.I_LAIHANLX == (int)eLaiHanLX.水资源销表)
            {                
                this.dtSource = new SpHelper().P_QS_BIAOJINXXHQ((int)this.LaiHanLX, laihanxx.S_LAIHANBH, BusinessTool.WType,laihanxx.S_YONGHUBH).Tables[0];
            }
            else if (laihanxx.I_LAIHANLX == (int)eLaiHanLX.水资源表过户)
            {
                dt = new SpHelper().P_QS_BIAOJINXXHQ((int)this.LaiHanLX, laihanxx.S_LAIHANBH, BusinessTool.WType, this.TargetYongHuH).Tables[0];
                dt.Columns.Add("S_YONGHUH");
                this.dtSource = dt;
            }
            else if (laihanxx.I_LAIHANLX == (int)eLaiHanLX.水资源销井)
            {
                dt = new SpHelper().P_QS_GUANLIANSBXXGET(0, BusinessTool.GetWaterYM(), laihanxx.S_BEIZHU).Tables[0];
                dt.Columns.Add("S_YONGHUH");
                this.dtSource = dt;
                //this.dtSource = new SpHelper().P_QS_JINBIAOHQ(3, null, laihanxx.S_BEIZHU).Tables[0];
            }
            else  if (laihanxx.I_LAIHANLX == (int)eLaiHanLX.水资源销户)
            {
                this.dtSource = new SpHelper().P_QS_BIAOJINXXHQ((int)this.LaiHanLX, laihanxx.S_LAIHANBH, BusinessTool.WType, laihanxx.S_YONGHUBH).Tables[0];
                this.BindData();
            }
            this.BindData();                  
            //如果为来函销表,勾选销表的项
            if (laihanxx.S_LAIHANBH != null)
            {
                this.LaiHanBH = laihanxx.S_LAIHANBH;
                //根据来函加载水表
                DataRowCollection drcTemp = null;
                if (laihanxx.I_LAIHANLX == (int)eLaiHanLX.水资源销表)
                {
                    drcTemp = new QS_LH_XIAOBIAOSBXXProcess().SelectQS_LH_XIAOBIAOSBXXesDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH, null).Rows;

                    Dictionary<string, object> shuibiaohs = new Dictionary<string, object>();
                    foreach (DataRow item in drcTemp)
                    {
                        shuibiaohs.Add(item["S_SHUIBIAOBH"].ToString(), item["S_SHUIBIAOBH"]);
                    }
                    //勾选对应的水表
                    for (int i = 0; i < this.gvList.VisibleRowCount; i++)
                    {
                        ASPxCheckBox cb = (ASPxCheckBox)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "cb");
                        if (null != cb)
                        {
                            HiddenField hid = (HiddenField)gvList.FindRowCellTemplateControl(i, (GridViewDataColumn)gvList.Columns[0], "hfShuiBiaoBH");
                            cb.Checked = shuibiaohs.ContainsKey(hid.Value);
                            //做过登记的,此时步骤为受理.不可修改了
                            if ((int)this.LaiHanLC < (int)eLaiHanLCBH.登记)
                            {
                                cb.Enabled = true;
                            }
                            else
                            {
                                cb.Enabled = false;
                            }
                        }
                    }
                }

            }


            if (Convert.ToInt32(this.LaiHanLC) >= 1)
            {
                this.gvList.Columns[3].Visible = false;
                this.gvList.Columns[0].Visible = false;
            }
        }        

        /*
        /// <summary>
        /// 获取申请信息
        /// </summary>
        /// <returns></returns>
        public List<object> GetShengQingXX()
        {
            List<object> list = new List<object>();            
            if (this.LaiHanLX == eLaiHanLX.水资源销表)
            {
                List<QS_LH_XIAOBIAOSBXX> shuibiaos = this.SelectedShuiBiaoList();
                for (int i = 0; i < shuibiaos.Count; i++)
                {
                    shuibiaos[i].S_LAIHANBH = this.LaiHanBH;
                    list.Add(shuibiaos[i]);
                }
            }
            else if (this.LaiHanLX == eLaiHanLX.水资源表过户)
            {
                QS_LH_BIAOGUOHXX obj = new QS_LH_BIAOGUOHXX();
                obj.S_LAIHANBH = this.LaiHanBH;
                obj.S_BIAOBH = this.S_SHUIBIAOBH ;
                obj.S_YUANYHH = this.YuanYongHuH;
                obj.S_MUBIAOYHH = this.TargetYongHuH;
                obj.S_MUBIAOJH = Convert.ToString(this.gvList.GetRowValues(this.gvList.FocusedRowIndex, "S_QUSHUIK"));
                list.Add(obj);
                //for (int i = 0; i < shuibiaobhs.Count; i++)
                //{
                   
                //}
            }
            return list;
        }
        */

        /// <summary>
        /// 添加申请信息
        /// </summary>
        /// <param name="laihanxx"></param>
        public void CreateShengQingXX(YS_LH_LAIHANXX laihanxx)
        {
            if (this.LaiHanLX == eLaiHanLX.水资源销表)
            {
                //先清除该来函旧的销表水表记录
                QS_LH_XIAOBIAOSBXXProcess bllT2 = new QS_LH_XIAOBIAOSBXXProcess();
                bllT2.DeleteQS_LH_XIAOBIAOSBXXesDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH);
                //添加销表水表记录
                //设置来函编号
                this.LaiHanBH = laihanxx.S_LAIHANBH;
                List<QS_LH_XIAOBIAOSBXX> shuibiaos = this.SelectedShuiBiaoList();
                for (int i = 0; i < shuibiaos.Count; i++)
                {
                    shuibiaos[i].S_LAIHANBH = this.LaiHanBH;
                    bllT2.InsertOrUpdate(shuibiaos[i]);
                }
            }
            else if (this.LaiHanLX == eLaiHanLX.水资源表过户)
            {
                //先清除该来函旧的销表水表记录
                QS_LH_BIAOGUOHXXProcess bllT2 = new QS_LH_BIAOGUOHXXProcess();
                bllT2.DeleteQS_LH_BIAOGUOHXXesDynamic("S_LAIHANBH=" + laihanxx.S_LAIHANBH);
                //添加销表水表记录
                //设置来函编号
                this.LaiHanBH = laihanxx.S_LAIHANBH;
                QS_LH_BIAOGUOHXX obj = new QS_LH_BIAOGUOHXX();
                obj.S_LAIHANBH = this.LaiHanBH;
                obj.S_BIAOBH = this.S_SHUIBIAOBH;
                obj.S_YUANYHH = this.YuanYongHuH;
                obj.S_MUBIAOYHH = this.TargetYongHuH;
                obj.S_MUBIAOJH = Convert.ToString(this.gvList.GetRowValues(this.gvList.FocusedRowIndex, "S_QUSHUIK"));
                bllT2.InsertOrUpdate(obj);
            } 
        }
        /// <summary>
        /// 用于从他处传值到本控件属性,此处用于登记前获取原用户与目标用户
        /// </summary>
        /// <param name="values"></param>
        public void RegistMemberValue(params object[] values)
        {
            this.YuanYongHuH = Convert.ToString(values[0]);
            this.TargetYongHuH = Convert.ToString(values[1]);
            this.S_SHUIBIAOBH = Convert.ToString(values[2]);
        }

        #endregion        
    }
}